﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualPress.Infrastructure.Entities.Entity;
using VirtualPress.Tool.Framework;

namespace VirtualPress.Infrastructure.Entities.Cache
{
    /// <summary>
    /// 参数只包含一个，为期刊Id
    /// </summary>
    public class OrganizationCache : IStorable<List<Organization>>
    {
        protected override bool ValidateParameter(params string[] keys)
        {
            return keys.Length == 1 && !String.IsNullOrEmpty(keys[0]);
        }

        protected override List<Organization> LoadDataItem(params string[] keys)
        {
            if (ValidateParameter(keys))
            {
                int journalId = keys[0].ToInt32();
                var list = new Organization().FindAll(c => c.JournalId == journalId);
                return list;
            }
            return null;
        }

        protected override string BuildKey(params string[] keys)
        {
            if (ValidateParameter(keys))
            {
                return String.Format("GetOrganizationList_{0}", keys[0]);
            }
            return string.Empty;
        }

        protected override string BuildKey(List<Organization> data)
        {
            if (data != null && data.Any())
            {
                foreach (var item in data) 
                {
                    bool isSame = data.Any(o => o.JournalId == item.JournalId);
                    if (!isSame)
                    {
                        return string.Empty;
                    }
                }
                return BuildKey(data.First().JournalId.ToString());
            }
            return string.Empty;
        }
    }
}
